package com.wfy.modules.openInterface.web;

import com.wfy.common.config.Global;
import com.wfy.common.entity.Result;
import com.wfy.common.entity.ResultGenerate;
import com.wfy.common.service.ServiceException;
import com.wfy.common.util.PageData;
import com.wfy.common.util.StringUtils;
import com.wfy.common.web.BaseController;
import com.wfy.modules.openInterface.service.SensorInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2017/9/27
 * Time: 20:21
 */
@Controller
@RequestMapping(value = "/v1/sensor")
public class SensorInfoController extends BaseController {

    // ================================================================
    // Constants
    // ================================================================

    // ================================================================
    // Fields
    // ================================================================

    @Resource
    private SensorInfoService sensorInfoService;


    // ================================================================
    // Constructors
    // ================================================================

    // ================================================================
    // Methods from/for super Interfaces or SuperClass
    // ================================================================

    // ================================================================
    // Public or Protected Methods
    // ================================================================

    /**
     * 修改传感器信息
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "根据传感器code，修改传感器信息")
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "code", required = true, value = "传感器编号", dataType = "string", paramType = "path"),
                    @ApiImplicitParam(name = "token", required = true, value = "平台颁发的KEY", dataType = "string", paramType = "header"),
                    @ApiImplicitParam(name = "monitorPoint", required = true, value = "监控点信息", dataType = "string", paramType = "body")
            }
    )
    @RequestMapping(value = "/{code}", method = RequestMethod.PATCH)
    @ResponseBody
    public Result update(@PathVariable String code, HttpServletRequest request) {
        String token = request.getHeader("token");
        PageData pd = getPageData();
        pd.put("code", code);
        pd.put("token", token);
        LOGGER.info(pd.toString());

        // 参数验证
        if (null == code) {
            return ResultGenerate.getResult(Global.MISSING_REQUIRED_PARAMS_MSG, Global.MISSING_REQUIRED_PARAMS_CODE);
        }
        String monitorPoint = pd.getString("monitorPoint");
        if (StringUtils.isBlank(monitorPoint)) {
            return ResultGenerate.getResult(Global.MISSING_REQUIRED_PARAMS_MSG, Global.MISSING_REQUIRED_PARAMS_CODE);
        }
        try {
            Result result = sensorInfoService.updateSensorInfo(pd);
            return result;
        } catch (ServiceException e) {
            LOGGER.error(e.getMessage());
            return ResultGenerate.getFailResult();
        }
    }


    // ================================================================
    // Getter & Setter
    // ================================================================

    // ================================================================
    // Private Methods
    // ================================================================


    // ================================================================
    // Inner or Anonymous Class
    // ================================================================

    // ================================================================
    // Test Methods
    // ================================================================

}
